package com.hzit.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.common.utils.PageUtils;
import com.hzit.common.utils.Query;

import com.hzit.shop.dao.GoodsDao;
import com.hzit.shop.entity.GoodsDescEntity;
import com.hzit.shop.entity.GoodsEntity;
import com.hzit.shop.entity.ItemEntity;
import com.hzit.shop.entity.SellerEntity;
import com.hzit.shop.entity.group.Goods;
import com.hzit.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private BrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );
        return new PageUtils(page);
    }
    //1. 保存商品
    @Override
    @Transactional
    public void save(Goods goods) {
        //1.1 向tb_goods表添加数据
        GoodsEntity goodsEntity = goods.getGoods();
        goodsEntity.setAuditStatus("0");
        this.save(goodsEntity);

        //1.2 向tb_goods_desc表添加数据
        GoodsDescEntity goodsDesc = goods.getGoodsDesc();
        goodsDesc.setGoodsId(goodsEntity.getId());
        goodsDescService.save(goodsDesc);

        //1.3 添加sku商品列表
        insertItemList(goods);
    }
    // 添加sku商品列表
    private void insertItemList(Goods goods) {
        List<ItemEntity> itemList = goods.getItemList();
        for (ItemEntity itemEntity : itemList) {
            //1.3.1 为sku商品添加商品id
            GoodsEntity goodsEntity = goods.getGoods();
            itemEntity.setGoodsId(goodsEntity.getId());
            //1.3.2 为itemEntity添加非空字段
            itemEntity.setTitle(goodsEntity.getGoodsName());
            itemEntity.setCategoryid(goodsEntity.getCategory3Id());
            itemEntity.setCreateTime(new Date());
            itemEntity.setUpdateTime(new Date());
            //1.3.3设置图片
            GoodsDescEntity goodsDesc = goods.getGoodsDesc();
            String itemImages = goodsDesc.getItemImages();
            List<Map> maps = JSON.parseArray(itemImages, Map.class);
            itemEntity.setImage(maps.get(0).get("url") + "");
            //1.3.4商家名称
            String sellerId = goodsEntity.getSellerId();
            SellerEntity seller = sellerService.getById(sellerId);
            itemEntity.setSeller(seller != null ? seller.getName() : "");
            //1.3.5分类名称
            String category = itemCatService.getById(goodsEntity.getCategory3Id()).getName();
            itemEntity.setCategory(category);

            //1.3.6品牌名字
            String brandId = goodsEntity.getBrandId();
            String name = brandService.getById(brandId).getName();
            itemEntity.setBrand(name);
            //1.3.3 添加sku商7品
            itemService.save(itemEntity);
        }
    }

    //2. 根据商品id查询商品
	@Override
	public Goods findById(String id) {
        //2.1 查询spu商品
        GoodsEntity goodsEntity = this.getById(id);
        //2.2 查询spu商品描述信息
        GoodsDescEntity goodsDescEntity = goodsDescService.getById(id);
        //2.3 在tb_item表中根据这个id作为外键进行查询sku商品列表
        List<ItemEntity> itemList = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        //2.4 返回组合对象
        return new Goods(goodsEntity,goodsDescEntity,itemList);
	}

	//3. 修改商品组合对象
    @Override
    @Transactional
    public void update(Goods goods) {
        //3.1 修改spu商品
        this.updateById(goods.getGoods());
        //3.2 修改spu商品描述
        goodsDescService.updateById(goods.getGoodsDesc());
        //3.3 根据商品id删除sku商品列表
        itemService.remove(new QueryWrapper<ItemEntity>().eq("goods_id",goods.getGoods().getId()));
        //3.4 添加sku商品列表
        List<ItemEntity> itemList = goods.getItemList();
        for (ItemEntity itemEntity : itemList) {
            itemEntity.setGoodsId(goods.getGoods().getId());
        }
        insertItemList(goods);

    }

}